package com.glsc.ngateway.platform.controller;

import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.api.flowable.dto.EventImproveTaskDto;
import com.glsc.ngateway.common.api.flowable.dto.FlowDoTaskRespDto;
import com.glsc.ngateway.common.api.flowable.dto.FlowParamSearchDto;
import com.glsc.ngateway.common.api.flowable.resp.ResponseSituationEventDto;
import com.glsc.ngateway.platform.service.EventImproveService;
import com.glsc.ngateway.platform.utils.RequestTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/eventImprove")
@Api(value = "事件整改流程接口", tags = {"事件整改流程接口"})
public class FlowEventImproveController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private EventImproveService eventImproveService;

    @Resource
    private RequestTool requestTool;


    /**
     * 流程发起
     *
     * @param eventImproveTaskDto 流程创建表单数据
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/createFlow")
    public PlatformResponse<FlowDoTaskRespDto> createFlow(@RequestBody EventImproveTaskDto eventImproveTaskDto) {
        PlatformResponse<FlowDoTaskRespDto> ret;
        try {
            String operatorStart = requestTool.getAccountFromRequest();
            FlowDoTaskRespDto respDto = eventImproveService.createFlow(eventImproveTaskDto, operatorStart);
            return PlatformResponse.successData(respDto);
        } catch (Exception e) {
            logger.error("createFlow！", e);
            return PlatformResponse.failedMsg("操作失败", e.getMessage());
        }
    }


    /**
     * 保存节点数据，但是不提交任务
     */
    @PostMapping("/saveFormData")
    @ApiOperation(value = "保存节点数据", notes = "保存节点数据")
    public PlatformResponse<FlowDoTaskRespDto> saveFormData(@RequestBody EventImproveTaskDto eventImproveTaskDto) {
        logger.info("保存节点数据，{}", eventImproveTaskDto);
        String operatorStart = requestTool.getAccountFromRequest();

        FlowDoTaskRespDto respDto = eventImproveService.saveFormData(eventImproveTaskDto, operatorStart);
        return PlatformResponse.successData(respDto);
    }


    /**
     * 审核通过
     */
    @PostMapping("/taskAuditPass")
    @ApiOperation(value = "审核通过", notes = "流程审核处理通过")
    public PlatformResponse<FlowDoTaskRespDto> taskAuditPass(@RequestBody EventImproveTaskDto taskDto) {
        logger.info("审核任务，" + taskDto);
        String operatorStart = requestTool.getAccountFromRequest();
        FlowDoTaskRespDto respDto = eventImproveService.taskAuditPass(taskDto, operatorStart);
        return PlatformResponse.successData(respDto);
    }

    /**
     * 审核驳回
     */
    @PostMapping("/taskAuditReject")
    @ApiOperation(value = "审核驳回，返回上一个节点", notes = "流程审核驳回处理")
    public PlatformResponse<FlowDoTaskRespDto> taskAuditReject(@RequestBody EventImproveTaskDto taskDto) {
        logger.info("驳回，返回上一个节点，" + taskDto);
        String operatorStart = requestTool.getAccountFromRequest();
        FlowDoTaskRespDto respDto = eventImproveService.taskAuditReject(taskDto, operatorStart);
        return PlatformResponse.successData(respDto);
    }

    /**
     * 根据流程实例ID或者任务ID检索流程
     *
     * @param taskDto processId 流程ID
     * @param taskDto taskId 任务ID
     */
    @PostMapping("/findByProcessIdOrTaskId")
    @ApiOperation(value = "根据流程实例ID或者任务ID检索流程", notes = "根据流程实例ID或者任务ID检索流程状态、备注等，如提供流程实例ID则认为只读方式获取数据")
    public PlatformResponse<EventImproveTaskDto> findByProcessIdOrTaskId(@RequestBody EventImproveTaskDto taskDto) {
        logger.info("查询流程内容，" + taskDto);
        EventImproveTaskDto respDto = eventImproveService.getWorkFlowFormByTaskId(taskDto);
        return PlatformResponse.successData(respDto);
    }


    @ApiOperation(value = "流程分页查询", notes = "流程分页查询")
    @RequestMapping("/findImproveProcessPage")
    public PlatformResponse<Page<ResponseSituationEventDto>> findImproveProcessPage(
            @ApiParam(value = "流程查询参数") @RequestBody(required = true) FlowParamSearchDto paramSearchDto) {
        logger.info("流程已办列表分页查询");
        String operatorStart = requestTool.getAccountFromRequest();
        Page<ResponseSituationEventDto> page = eventImproveService.findImproveProcessPage(paramSearchDto.getPageNo(), paramSearchDto.getPageSize(), paramSearchDto,operatorStart);
        return PlatformResponse.successData(page);
    }
}
